let F = {};
F.define = function (str, fn) {
    let parts = str.split('.'),
        old = parent = this;
    if (parts[0] === 'F') {
        parts = parts.slice(1);
    }
    if (parts[0] === 'define' || parts[0] === 'module') {
        return;
    }
    for (let i = 0; i < parts.length; i++) {
        if (typeof parent[parts[i]] === 'undefined') {
            parent[parts[i]] = {};
        }
        old = parent;
        parent = parent[parts[i]];
    }
    if (fn) {
        old[parts[parts.length-1]] = fn();
    }
    return this;
};

F.define('string', function () {
    return {
        trim: function (str) {
            return str.replace(/^\s+|\s+$/g, '');
        }
    }
});
console.log(F.string.trim('   测试用例     '));

F.define('dom', function () {
    let $ = function (id) {
        $.dom = document.getElementById(id);
        return $;
    };
    $.html = function (html) {
        if (html) {
            this.dom.innerHTML = html;
            return this;
        } else {
            return this.dom.innerHTML;
        }
    };
    return $;
});
console.log(F.dom('test').html());
F.define('dom.addClass');
F.dom.addClass = function (type, fn) {
    return function (className) {
        if (this.dom.className.indexOf(className) === -1) {
            this.dom.className += ' ' + className;
        }
    }
}();
F.dom('test').addClass('test');

F.module = function () {
    let args = [].slice.call(arguments),
        fn = args.pop(),
        parts = args[0] && args[0] instanceof Array ? args[0] : args,
        modules = [],
        modIDs = '',
        parent;
    for (let i = 0; i < parts.length; i++) {
        if (typeof parts[i] === 'string') {
            parent = this;
            modIDs = parts[i].replace(/^F\./, '').split('.');
            for (let j = 0; j < modIDs.length; j++) {
                parent = parent[modIDs[j]] || false;
            }
            modules.push(parent);
        } else {
            modules.push(parts[i]);
        }
    }
    fn.apply(null, modules);
};
F.module(['dom', document], function (dom, doc) {
    dom('test').html('   new add!     ');
    doc.body.style.background = 'red';
});
F.module('dom', 'string.trim', function (dom, trim) {
    let html = dom('test').html();
    let str = trim(html);
    console.log('*' + html + '*', '*' + str + '*');
});